#!/usr/bin/python2.4
# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
# 
# $Id: xhtml.py 2 2007-06-04 12:27:36Z keith.dart $
#
#    Copyright (C) 1999-2006  Keith Dart <keith@kdart.com>
#
#    This library is free software; you can redistribute it and/or
#    modify it under the terms of the GNU Lesser General Public
#    License as published by the Free Software Foundation; either
#    version 2.1 of the License, or (at your option) any later version.
#
#    This library is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#    Lesser General Public License for more details.

"""
Test module for the XML.XHTML module.

"""

import sys

import XML.POM as POM
import WWW.XHTML as XHTML

import webbrowser

FNAME = "/tmp/testXHTML.html"

def test(cf):
    htd = XHTML.new_document(XHTML.STRICT)
    htd.title = "This is the title."
    htd.add_header(1, 'Main document & "stuff"')
    htd.new_para("This is a test. This is text.")
    htd.add_unordered_list(["List line one.", "list line two."])
    BR = htd.get_new_element("Br")
    A = htd.get_new_element("A", href="somelink.html")
    A.add_text("some link")
    p = htd.get_para()
    p.append(A)
    p.add_text(" This is ")
    b = p.bold("bold")
    p.add_text(" text. using ")
    stb = htd.get_new_element("B")
    stb.add_text("bold tags")
    p.text(stb)
    rp = str(p)
    htd.append(POM.ASIS(rp))
    t = htd.add_table(border=1)
    t.summary = "This is a test table."
    t.caption("table caption")
    h = t.set_heading(2, "heading col 2")
    h.set_attribute("class", "headerclass")
    t.set_heading(1, "heading col 1")
    t.set_cell(1,1,"row 1, col 1")
    t.set_cell(1,2,"row 2, col 1")
    t.set_cell(2,1,"row 1, col 2")
    t.set_cell(2,2,"row 2, col 2")
    div = htd.get_section("section1")
    div.add_header(1, "Div heading.")
    div.new_para("First div para.")
    htd.append(div)
    div2 = div.get_section("section2")
    div2.new_para("Second div para")
    div.append(div2)

    dl = div.add_definition_list()
    dl.add_definitions({"def1":"The definition of 1", 
                    "def2": "The definition of 2"})

    # create method
    NM = htd.nodemaker
    ul = NM("Ul", None, 
            NM("Li", None, "line 1"), 
            NM("Li", None, "line 2")
            )
    htd.append(ul)
    creator = htd.creator
    parts = creator([("Just", "just/"), "How will this turn out?", ["It is hard to tell.", "Well, not too hard."]])

    htd.add_comment("the name attribute is required for all but submit & reset")
    htd.append(parts)
    f = htd.add_form(action="http://localhost:4001/cgi-bin/testing.py", method="post")

    f.add_textarea("mytextarea", """Default text in the textarea.""") ; f.append(BR)
    f.add_input(type="text", name="mytext", value="mytext text") ; f.append(BR)
    f.add_input(type="button", name="button1", src="button.png", value="Button") ; f.append(BR)
    f.add_input(type="submit", name="submit1", src="submit.png", value="Ok") ; f.append(BR)
    f.add_radiobuttons("radlist", ["one", "two", "three", "four"], vertical=False) ; f.append(BR)
    f.add_checkboxes("checks", ["one", "two", "three", "four"], vertical=True) ; f.append(BR)
    f.add_fileinput(name="myfile", default="/etc/hosts") ; f.append(BR)
    f.add_textinput(name="mytext", label="Enter text") ; f.append(BR)
    f.yes_no("What's it gonna be?")
    f.add_select(["one", "two", ("three", True), "four", 
                   {"agroup": ["group1", "group2"]}], 
                   name="myselect") ; f.append(BR)

    f.add_select({"Group1": Enums("g1one", "g1two", "g1three")+[("g1four", True)],
                  "Group2": Enums("g2one", "g2two", "g2three"),
                  "Group3": Enums("g3one", "g3two", "g3three"),
                }, name="useenums") ; f.append(BR)

    f.add_select(["mone", "mtwo", ("mthree", True), ("mfour", True)], name="multiselect", multiple=True) ; f.append(BR)

    set = f.add_fieldset("afieldset")
    set.add_textinput(name="settext", label="Enter set text")
    set.add_textinput(name="settext2", label="Enter set text 2", default="Default text.")
    set.append(BR)
    tbl = htd.new_table([1,2,3,4,5], 
                        [NULL, NULL, NULL], 
                        ["col1", "col2", "col3"], width="100%", summary="autogenerated")

    # object 
    subdoc = XHTML.new_document(XHTML.STRICT)
    parts = subdoc.creator(("Add a document object.", ["Some data.", "some more data.."]))
    subdoc.append(parts)
    sdfo = open("/tmp/subdoc.html", "w")
    subdoc.emit(sdfo)
    sdfo.close()
    htd.add_object(data="subdoc.html", type=subdoc.MIMETYPE,
                                width="400px", height="600px")
    htd.emit(sys.stdout)
    print "-----"
    fo = open(FNAME, "w")
    bw = POM.BeautifulWriter(fo, XHTML.INLINE)
    htd.emit(bw)
    fo.close()
    print "----- Form values:"
    print f.fetch_form_values()
    print "----- Form elements:"
    felems = f.fetch_form_elements()
    for name, elemlist in felems.items():
        print repr(name), ": ", repr(elemlist)
        print
    webbrowser.open("file://%s" % (FNAME,))

#   print "---------------------"
#   xhtml = get_document("/usr/share/doc/apache-2.0.49-r3/manual/index.html.en")
#   fo = open("/tmp/apache_manual_mod_index.html", "wb")
#   xhtml.emit(fo)
#   fo.close()



def run(cf):
    test(cf)
